package com.dbkj.axb;

import com.alibaba.fastjson.JSONArray;
import com.dbkj.common.model.Axb;
import com.dbkj.common.utils.RedisUtils;
import com.jfinal.kit.Kv;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.SqlPara;

import java.util.Arrays;

/**
 * @Author: Max
 * @Date: 2019/10/29 19:07
 */

public class AxbService {

    private static final Axb dao = new Axb().dao();


    public Page<Axb> paginate(Integer pageNumber, Integer pageSize, String telX,String companyName,String expiration,String areaCode,String concurrent,String aGroup,String groupId,String remark) {
        SqlPara sql = dao.getSqlPara("telx.show", Kv.by("telX",telX).set("companyName",companyName).set("expiration",expiration).set("areaCode",areaCode).set("concurrent",concurrent).set("aGroup",aGroup).set("groupId",groupId).set("remark",remark));
        return dao.paginate(pageNumber,pageSize,sql);
    }

    public void delete(Integer paraToInt) {
        Axb x = dao.findById(paraToInt);
        x.delete();
        if(PropKit.getBoolean("is_cluster")){
            RedisUtils.cluster.del("telx_"+x.getTelX()+"_concurrent_list");
        }else{
            RedisUtils.jedis.del("telx_"+x.getTelX()+"_concurrent_list");
        }
    }

    public void deleteBatch(JSONArray ids) {
        for(int i=0;i<ids.size();i++){
            Axb x = dao.findById(ids.getInteger(i));
            x.delete();
            if(PropKit.getBoolean("is_cluster")){
                RedisUtils.cluster.del("telx_"+x.getTelX()+"_concurrent_list");
            }else{
                RedisUtils.jedis.del("telx_"+x.getTelX()+"_concurrent_list");
            }
        }
    }


    public void updateBatch(String telX, String companyName, String expiration, String areaCode, String concurrent, String aGroup, String groupId, String remark, String key, String value) {

        if(StrKit.isBlank(telX) && StrKit.isBlank(companyName) && StrKit.isBlank(expiration) && StrKit.isBlank(areaCode) && StrKit.isBlank(concurrent) && StrKit.isBlank(aGroup) && StrKit.isBlank(groupId) && StrKit.isBlank(remark)){
            return;
        }
        SqlPara sql = dao.getSqlPara("telx.updateBatch",Kv.by("tel_x",telX)
                .set("company_name",companyName)
                .set("expiration",expiration)
                .set("area_code",areaCode)
                .set("concurrent",concurrent)
                .set("a_group",aGroup)
                .set("group_id",groupId).set("remark",remark).set("value",value).set("key2",key));
        //dao.find(sql);
        Db.update(sql);
    }

    public boolean hasConf(Integer confId){
        Axb first = dao.findFirst("select * from axb where conf_id =?", confId);
        if(first==null){
            return false;
        } else {
            return true;
        }
    }

    public boolean hasGroup(Integer groupId){
        Axb x = dao.findFirst("select * from axb where group_id =?",groupId);
        if(x == null){
            return false;
        } else {
            return true;
        }
    }
}
